<template>
    <div class="page-container-tab" v-loading="loading">
        <div class="page-filter-option mb-3">
            <el-form :inline="true" :model="filterOption" class="flex flex-wrap">
                <el-form-item>
                    <component-page-org-tree-option-check v-model="filterOption.plat_code" @change="filterChange"></component-page-org-tree-option-check>
                </el-form-item>
                <el-form-item class="small">
                    <el-select :placeholder="$t('game.other.mail.type')" v-model="filterOption.type" clearable @change="filterChange">
                        <el-option v-for="(item, index) in $t('game.other.mail.typeList')" :key="index" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item class="small">
                    <el-select :placeholder="$t('game.other.mail.issend')" v-model="filterOption.issend" clearable @change="filterChange">
                        <el-option v-for="(item, index) in $t('game.other.mail.issendList')" :key="index" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="filterOption.datetime_column" @change="filterChange" style="width: 110px;">
                        <el-option :label="$t('game.other.mail.send_time')" value="send_time"></el-option>
                        <el-option :label="$t('game.other.mail.expire_time')" value="expire_time"></el-option>
                        <el-option :label="$t('game.other.mail.created')" value="created"></el-option>
                    </el-select>
                    <component-page-option-datetime-range type="datetimerange" align="center" defaultShortcuts
                                                          v-model="filterOption.datetime" @change="filterChange">
                    </component-page-option-datetime-range>
                </el-form-item>
                <el-form-item>
                    <el-button @click="filterChange">{{ $t('action.search') }}</el-button>
                </el-form-item>
                <el-form-item class="ml-auto mr-0">
                    <el-button type="primary" plain icon="el-icon-plus" @click="dialogItemCreate">{{ $t('action.new') }}</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="page-container-inner-fluid flex-auto flex flex-col">
            <component-page-content-resize v-slot="{ content }">
                <el-table style="width: 100%" :height="content.height"
                          :data="dataList"
                          :default-sort="{prop: filterOption.order_column, order: filterOption.order_key}"
                          @sort-change="filterOrderChange"
                          highlight-current-row>
                    <el-table-column min-width="200" :label="$t('platform.setting.tree.org')" fixed="left">
                        <template slot-scope="scope">{{ scope.row.org.name || scope.row.plat_code }}</template>
                    </el-table-column>
                    <el-table-column min-width="150" :label="$t('game.other.mail.type')" prop="type">
                        <template slot-scope="scope">
                            <el-tag effect="plain" size="mini" type="info">
                                {{ $t(`game.other.mail.typeList.${scope.row.type}.label`) || scope.row.type }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column min-width="200" :label="$t('game.other.mail.title')">
                        <template slot-scope="scope">
                            <div v-for="(title,index) in scope.row.titles" v-if="!index" v-html="title.content"></div>
                        </template>
                    </el-table-column>
                    <el-table-column min-width="300" :label="$t('game.other.mail.body')">
                        <template slot-scope="scope">
                            <div v-for="(body,index) in scope.row.bodys" v-if="!index" v-html="body.content_format"></div>
                        </template>
                    </el-table-column>
                    <el-table-column min-width="150" :label="$t('game.other.mail.issend')" prop="issend">
                        <template slot-scope="scope">
                            <el-tag effect="plain" size="mini" type="info">
                                {{ $t('game.other.mail.issendList')[scope.row.issend].label }}
                            </el-tag>
                            <span class="operation-options-icon" v-if="!scope.row.issend">
                                <i class="el-icon-edit" @click="dialogItemSendMail(scope)"></i>
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column min-width="220" :label="$t('game.other.mail.send_time')" prop="send_time" sortable="custom" :sort-orders="['descending', 'ascending']">
                        <template slot-scope="scope">
                            <div v-if="!scope.row.send_time">----</div>
                            <component-page-timestamp :timestamp="scope.row.send_time" v-else></component-page-timestamp>
                        </template>
                    </el-table-column>
                    <el-table-column min-width="220" :label="$t('game.other.mail.expire_time')" prop="expire_time" sortable="custom" :sort-orders="['descending', 'ascending']">
                        <template slot-scope="scope">
                            <component-page-timestamp :timestamp="scope.row.expire_time"></component-page-timestamp>
                        </template>
                    </el-table-column>
                    <el-table-column min-width="220" :label="$t('game.other.mail.created')" prop="created" sortable="custom" :sort-orders="['descending', 'ascending']">
                        <template slot-scope="scope">
                            <component-page-timestamp :timestamp="scope.row.created"></component-page-timestamp>
                        </template>
                    </el-table-column>
                    <el-table-column width="100" :label="$t('game.other.mail.operation')" align="right" fixed="right">
                        <template slot-scope="scope">
                            <span class="operation-options-icon">
                                <i class="el-icon-view" @click="drawerItemDetails(scope)"></i>
                            </span>
                            <span class="operation-options-icon" v-if="!scope.row.issend">
                                <i class="el-icon-edit-outline" @click="dialogItemUpdate(scope)"></i>
                            </span>
                            <span class="operation-options-icon">
                                <i class="el-icon-delete" @click="dialogItemDestroy(scope)"></i>
                            </span>
                        </template>
                    </el-table-column>
                </el-table>
            </component-page-content-resize>
            <div class="text-right p-4">
                <el-pagination background layout="total, sizes, prev, pager, next, jumper"
                               :current-page="filterOption.page"
                               :page-size.sync="filterOption.page_size"
                               :total="dataMeta.total"
                               @size-change="filterChange"
                               @current-change="filterPageChange">
                </el-pagination>
            </div>
        </div>
        <el-dialog :title="$t('game.other.mail.dialog.sendMail')" center width="320px" :visible.sync="dialog.visible.sendMail">
            <div class="text-center" v-html="$t('confirm.mail', { status: $t('action.send') })"></div>
            <div slot="footer">
                <el-button type="primary" @click="sendMail">{{ $t('action.confirm') }}</el-button>
                <el-button @click="dialog.visible.sendMail = false">{{ $t('action.cancel') }}</el-button>
            </div>
        </el-dialog>
        <el-dialog :title="$t('game.other.mail.dialog.destroy')" center width="320px" :visible.sync="dialog.visible.destroy">
            <div class="text-center" v-html="$t('confirm.mail', { status: $t('action.delete') })"></div>
            <div slot="footer">
                <el-button type="danger" @click="destroy">{{ $t('action.confirm') }}</el-button>
                <el-button @click="dialog.visible.destroy = false">{{ $t('action.cancel') }}</el-button>
            </div>
        </el-dialog>
        <item-create :visible.sync="dialog.visible.create" @create="filterChange"></item-create>
        <item-update :visible.sync="dialog.visible.update" :data="itemCache" @update="filterChange"></item-update>
        <item-details :visible.sync="drawer.visible.details" :data="itemCache"></item-details>
    </div>
</template>

<script>
import ItemCreate from './ItemCreate.vue'
import ItemUpdate from './ItemUpdate.vue'
import ItemDetails from './ItemDetails.vue'

export default {
    components: {
        ItemCreate,
        ItemUpdate,
        ItemDetails,
    },
    props: ['activeName'],
    data() {
        return {
            initStatus: false,
            loading: false,
            filterOption: {
                plat_code: [],
                datetime: null,
                datetime_column: 'created',
                type: null,
                issend: null,
                page: 1,
                page_size: 30,
                order: 'desc',
                order_key: 'descending',
                order_column: 'created',
            },
            dataMeta: {
                total: 0,
            },
            dataList: [],
            itemCache: {},
            dialog: {
                visible: {
                    create: false,
                    update: false,
                    sendMail: false,
                    destroy: false,
                }
            },
            drawer: {
                visible: {
                    details: false,
                }
            }
        }
    },
    watch: {
        activeName: {
            immediate: true,
            handler(n, o) {
                if (n == 'server' && !this.initStatus) {
                    // 标记初始化
                    this.initStatus = true;
                    // 初始化数据
                    this.filterChange();
                }
            }
        },
    },
    methods: {
        clearFilterOption() {
            this.loading = true;
            this.filterOption.page = 1;
        },
        filterChange() {
            this.clearFilterOption();
            this.getDataList();
        },
        filterPageChange(page) {
            this.loading = true;
            this.filterOption.page = page;
            this.getDataList();
        },
        filterOrderChange(scope) {
            this.clearFilterOption();
            this.filterOption.order = scope.order == 'ascending' ? 'asc' : 'desc';
            this.filterOption.order_key = scope.order;
            this.filterOption.order_column = scope.prop;
            this.getDataList();
        },
        getDataList() {
            axios.get('/game/other/mail/server/getList', {
                params: this.filterOption
            }).then((response) => {
                if (response.data.resp_msg.code == 200) {
                    this.dataMeta = response.data.resp_data.meta;
                    this.dataList = response.data.resp_data.data;
                } else {
                    this.$message({
                        type: 'error',
                        message: response.data.resp_msg.message,
                        showClose: true
                    });
                }
                this.loading = false;
            })
        },
        // 发送邮件
        sendMail() {
            // 关闭提示框
            this.dialog.visible.sendMail = false;
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/game/other/mail/server/sendMail', {
                id: this.itemCache.id
            }).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.send')}),
                        showClose: true
                    });
                    // 刷新列表
                    this.filterChange();
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.send')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
        // 删除数据
        destroy() {
            // 关闭提示框
            this.dialog.visible.destroy = false;
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/game/other/mail/server/destroy', {
                id: this.itemCache.id
            }).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.delete')}),
                        showClose: true
                    });
                    // 刷新列表
                    this.filterChange();
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.delete')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
        // 显示模态框（新增）
        dialogItemCreate() {
            this.dialog.visible.create = true;
        },
        // 显示模态框（编辑）
        dialogItemUpdate(scope) {
            // 缓存数据
            this.itemCache = scope.row;
            // 显示模态框
            this.dialog.visible.update = true;
        },
        // 显示模态框（发送邮件）
        dialogItemSendMail(scope) {
            // 缓存数据
            this.itemCache = scope.row;
            // 显示模态框
            this.dialog.visible.sendMail = true;
        },
        // 显示模态框（删除）
        dialogItemDestroy(scope) {
            // 缓存数据
            this.itemCache = scope.row;
            // 显示模态框
            this.dialog.visible.destroy = true;
        },
        // 打开抽屉（详情）
        drawerItemDetails(scope) {
            // 缓存数据
            this.itemCache = scope.row;
            // 显示抽屉
            this.drawer.visible.details = true;
        },
    }
}
</script>

<style lang="scss">

</style>
